from core.support import set_cutscene
from core.item import Item
from core.action import Action
from core.support import set_cutscene, get_game


class Flashlight(Item):
    def __init__(self):
        Item.__init__(self, "Flashlight")    # do basic initialization for every item
        self.is_lit = False
        self.has_batteries = False
        
        # build list of actions
        self.add_action(TurnOn)
        self.add_action(TurnOff)
        self.add_action(InsertBatteries)
    
    def get_description(self):
        if self.is_lit:
            return "Flashlight (shining brightly)"
        else:
            return "Flashlight (off)"


# define actions for this item

class TurnOn(Action):
    def __init__(self):
        Action.__init__(self, "turn_on")    # do basic initialization for every action
    
    # return description of action (used in label on webpage)
    def get_description(self):
        return "Turn Flashlight On"
    
    # return http method to use when user clicks on this action
    # use "get" if just moving to another room.  if changing something
    # like the state of an inventory item or room then use "post"
    def get_method(self):
        return "post"
    
    # enabled only if lamp is off.
    def is_enabled(self):
        if self.item.has_batteries and self.item.is_lit == False:
            return True
        else:
            return False
    
    # turn lamp on and add cutscene.
    # We changed this code here for the project.
    def do_action(self,game,room,request):
        self.item.is_lit = True
        set_cutscene("You press the switch on the flashlight and it turns on")
        return

class TurnOff(Action):
    def __init__(self):
        Action.__init__(self, "turn_off")    # do basic initialization for every action
    
    # return description of action (used in label on webpage)
    def get_description(self):
        return "Turn Flashlight Off"
    
    # return http method to use when user clicks on this action
    # use "get" if just moving to another room.  if changing something
    # like the state of an inventory item or room then use "post"
    def get_method(self):
        return "post"
    
    # enabled only if lamp is on.
    def is_enabled(self):
        if self.item.is_lit:
            return True
        else:
            return False
    
    # turn lamp on and add cutscene
    def do_action(self,game,room,request):
        self.item.is_lit = False
        set_cutscene("You press the switch on the flashlight and it turns off")
        return
    
class InsertBatteries(Action):
    def __init__(self):
        Action.__init__(self, "insert")
    
    # return description of action (used in label on webpage)
    def get_description(self):
        return "Insert Batteries into Flashlight"
    
    def get_method(self):
        return "post"
    
    def is_enabled(self):
        game = get_game()
        
        player = game.get_player()
        if 'Flashlight' in player.inventory and 'Batteries' in player.inventory:
            return True
        else:
            return False
    
    def do_action(self,game,room,request):
        game = get_game()
        player = game.get_player()
        set_cutscene("Your batteries are installed and your flashlight is ready for use!")
        player.inventory.pop('Batteries', None)
        self.item.has_batteries = True
        return
    